package com.fafi.selfmvc.validator;

import com.fafi.selfmvc.vo.Address;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class AddressValidator implements Validator {
	@Override
	public boolean supports(Class<?> clazz) {
		return Address.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmpty(errors, "location", "location.empty");
		Address p = (Address) target;
		if (p != null && p.getLocation() != null && p.getLocation().length() > 5) {
			errors.rejectValue("location", "value too length", "长度不能超过5");
		}
	}
}
